package com.example.demo.service.impl;

import com.example.demo.dao.BookDao;
import com.example.demo.entity.Book;
import com.example.demo.mapper.BookMapper;
import com.example.demo.service.BookService;
import com.example.demo.sharding.RouteModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookDao bookDao;

    @Override
    public List<Book> list() {
        //指定数据源和表名
        RouteModel routeModel = RouteModel.builder()
                .dbID(0)
                .tableName("book")
                .build();
        return bookMapper.listByRout(routeModel);

        //不指定数据源，则使用默认数据源
        //return bookDao.list();
    }

    @Override
    public Book getByNum(String num) {

        //不指定数据源和表名，注解中通过num去查询所属库和表
        RouteModel routeModel = RouteModel.builder()
                .num(num)
                .tablePrefix("book")
                .build();

        return bookDao.findByRout(routeModel);

    }
}
